<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * HITI Web Develop
 *
 * @package     HITI Web Dev
 * @category    Models
 * @see 		Hiti common
 * @author      PHUOCNGUYEN - HITI Dev Team
 * @copyright   Copyright (c) 2013, HITI Corp
 */

class Hiti_common_model extends CI_Model {
	var $lng;

	public function __contruct()
	{
		parent::__contruct();
	}

	public function hiti_init()
	{
		$GLOBALS['cfg'] = array();
		$this->db->query('SET NAMES "LATIN1"');
		$this->db->select('keyword, value');
		$query = $this->db->get('config');
		foreach ($query->result() as $val)
		{
			$GLOBALS['cfg'][$val->keyword] = $val->value;
		}
		$this->lng = $this->session->userdata('lng');
		if (!$this->lng)
		{
			$this->session->set_userdata(array('lng' => 'vn'));
			$this->lng = $this->session->userdata('lng');
		}
		$GLOBALS['cfg']['company_name'] = $GLOBALS['cfg']['company_name_'.$this->lng];
		$GLOBALS['cfg']['site_des'] = $GLOBALS['cfg']['site_des_vn'];
		$GLOBALS['cfg']['site_title'] = $GLOBALS['cfg']['site_title_'.$this->lng];
		$GLOBALS['cfg']['address'] = $GLOBALS['cfg']['address_'.$this->lng];
		$GLOBALS['cfg']['lng'] = $this->lng;
		$GLOBALS['cfg']['image'] = base_url().'images/logo.png';
		$GLOBALS['var']['mem_logged'] = $this->session->userdata('mem_logged', true);
		$GLOBALS['var']['mem_email'] = $this->session->userdata('mem_email', true);
		if ($GLOBALS['var']['mem_logged'] && $GLOBALS['var']['mem_email'] != '')
		{
			$this->db->select('id, name, email, avartar');
			$this->db->where('email', $GLOBALS['var']['mem_email']);
			$GLOBALS['mem'] = $this->db->get('members')->row_array();
		}
		if (!empty($GLOBALS['mem']['avartar']))
		{
			$GLOBALS['mem']['avartar'] = 'upload/avatars/'.$GLOBALS['mem']['avartar'];
		}
        else
        {
        	if (!empty($GLOBALS['mem']['gioitinh']))
        	{
        		$GLOBALS['mem']['avartar'] = 'images/ava'.($GLOBALS['mem']['gioitinh'] ? 'nam' : 'nu').'.jpg';
        	}
        	else
        	{
        		$GLOBALS['mem']['avartar'] = 'images/noava.jpg';
        	}
        }
		if (!$this->session->userdata('fb_state'))
        {
        	$this->session->set_userdata(array('fb_state' => md5(uniqid(rand(), TRUE))));
        }
		$GLOBALS['var']['show_mode_list'] = $this->input->cookie('show_mode_list', true);
		if ($GLOBALS['var']['show_mode_list'] == '')
		{
			$GLOBALS['var']['show_mode_list'] = $GLOBALS['cfg']['show_mode_list'];
		}
		$GLOBALS['var']['default_sort_product'] = $this->input->cookie('default_sort_product', true);
		$GLOBALS['var']['order_mode'] = 'DESC';
		$GLOBALS['var']['order_by'] = 'id';
		if ($GLOBALS['var']['default_sort_product'] == '')
		{
			$GLOBALS['var']['default_sort_product'] = empty($GLOBALS['cfg']['default_sort_product']) ? 'n-1' : $GLOBALS['cfg']['default_sort_product'];
		}
		if($GLOBALS['var']['default_sort_product'] == 'by_pass') {
		    $GLOBALS['var']['order_mode'] = 'DESC';
		    $GLOBALS['var']['order_by'] = 'order_active';
		}
		if($GLOBALS['var']['default_sort_product'] == 'n-1') {
		    $GLOBALS['var']['order_mode'] = 'DESC';
		    $GLOBALS['var']['order_by'] = 'id';
		}
		if($GLOBALS['var']['default_sort_product'] == '1-n') {
		    $GLOBALS['var']['order_mode'] = 'ASC';
		    $GLOBALS['var']['order_by'] = 'id';
		}
		if($GLOBALS['var']['default_sort_product'] == 'a-z') {
		    $GLOBALS['var']['order_mode'] = 'ASC';
		    $GLOBALS['var']['order_by'] = 'name_vn';
		}
		if($GLOBALS['var']['default_sort_product'] == 'z-a') {
		    $GLOBALS['var']['order_mode'] = 'DESC';
		    $GLOBALS['var']['order_by'] = 'name_vn';
		}
		if($GLOBALS['var']['default_sort_product'] == 'max-min') {
		    $GLOBALS['var']['order_mode'] = 'DESC';
		    $GLOBALS['var']['order_by'] = 'gia2';
		}
		if($GLOBALS['var']['default_sort_product'] == 'min-max') {
		    $GLOBALS['var']['order_mode'] = 'ASC';
		    $GLOBALS['var']['order_by'] = 'gia2';
		}
		$GLOBALS['var']['default_limit_product'] = $this->input->cookie('default_limit_product', true);
		if ($GLOBALS['var']['default_limit_product'] == '')
		{
			$GLOBALS['var']['default_limit_product'] = empty($GLOBALS['cfg']['default_limit_product']) ? 20 : $GLOBALS['cfg']['default_limit_product'];
		}
		$GLOBALS['var']['act'] = $this->uri->segment(1);
		$GLOBALS['var']['do'] = $this->uri->segment(2);
		if (count(explode('page-', $GLOBALS['var']['do'])) > 1)
		{
			$GLOBALS['var']['do'] = '';
		}
		$GLOBALS['var']['id'] = '';
		$GLOBALS['var']['q'] = $this->input->get('q', true);
		$GLOBALS['var']['rowstart'] = end(explode('page-', current_url()));
		if($GLOBALS['var']['rowstart'] == 0) $GLOBALS['var']['rowstart'] = 1;
		$GLOBALS['lng'] = array();
		$this->db->query('SET NAMES "LATIN1"');
		$this->db->select('keyword, '.$this->lng);
		$query = $this->db->get('languages');
		foreach ($query->result() as $val) {
			$GLOBALS['lng'][$val->keyword] = $val->{$this->lng};
		}
		$time = time();
		$ip = $this->input->ip_address();
		if($this->input->cookie('visit') != 1)
		{
			setcookie('visit', 1, $time + COOKIE_TIME, '/');
			$this->load->library('user_agent');
			if ($this->agent->is_browser())
			{
			    $browser = $this->agent->browser();
			}
			elseif ($this->agent->is_robot())
			{
			    $browser = $this->agent->robot();
			}
			elseif ($this->agent->is_mobile())
			{
			    $browser = $this->agent->mobile();
			}
			else
			{
			    $browser = 'unknown';
			}
			if (!$this->agent->is_robot())
			{
				$this->db->select('id');
				$this->db->where('online_ip', $ip);
				$this->db->where('online_lastactive >=', $time - COOKIE_TIME);
				if (!$this->db->get('online')->num_rows())
				{
					$ipinfo = json_decode(@file_get_contents('http://ip-api.com/json/'.$ip));
					if (!empty($ipinfo))
					{
						if (array_key_exists('status', $ipinfo) && $ipinfo->status == 'success')
						{
							$ipinfo->loc = $ipinfo->lat.','.$ipinfo->lon;
							$referer = $this->input->server('HTTP_REFERER');
							$backlink = '';
							if ($referer)
							{
								$parse_url = parse_url($referer);
								if (is_array($parse_url) && count($parse_url))
								{
									$backlink = $parse_url['host'];
								}
							}
							$data = array(
								'online_ip' => $ip,
								'online_lastactive' => $time,
								'date_added' => date('Y-m-d H:i:00', $time),
								'day' =>  date('d', $time),
								'month' =>  date('m', $time),
								'year' =>  date('Y', $time),
								'trinhduyet' => $browser,
								'version' => $this->agent->version(),
								'hdh' => $this->agent->platform(),
								'loc' => $ipinfo->loc,
								'org' => $ipinfo->org,
								'city' => $ipinfo->city,
								'region' => $ipinfo->regionName,
								'country' => $ipinfo->countryCode,
								'referer' => $referer,
								'backlink' => $backlink
							);
							$this->db->insert('online', $data);
						}
					}
				}
			}
		}
		$this->db->where('online_ip', $ip);
		$this->db->where('online_lastactive >=', $time - COOKIE_TIME);
		$this->db->update('online', array('online_lastactive' => $time));
	}

	public function header_templ()
	{
		return array(
			'logo_data' => $this->logo(),
			'categories_menu' => $this->cat_menu(),
			'num_item_cart' => $this->num_item_cart(),
			'list_partners' => $this->partners(),
			'online_info' => $this->online_info(),
			'right_adv' => $this->adv_box('RIGHT'),
			'left_adv' => $this->adv_box('LEFT'),
			'slider_box' => $this->slider_box(),
			'newscat_menu' => $this->newscat_menu('', '', '', 'menu_top'),
			'list_ram' => $this->list_property('chatlieu'),
			'list_manhinh' => $this->list_property('size'),
			'list_camera' => $this->list_property('dvt'),
			'news_menu' => $this->news_menu('', '', 'id'),
			'list_slider' => $this->list_slider(),
			'hinhthucmuahang_info' => $this->news_info($GLOBALS['cfg']['hinhthucmuahang_pageid']),
		);
	}

	public function footer_templ()
	{
		return array(
			'list_tags' => $this->list_tags(),
			'newscat_menu' => $this->newscat_menu('', '', '', 'menu_footer'),
			'news_menu' => $this->news_menu('', '', 'order_footer', 'menu_footer'),
			'news_box' => $this->news_menu($GLOBALS['cfg']['tintuc_catid'], 5),
			'gioithieu_info' => $this->news_info($GLOBALS['cfg']['gioithieu_pageid']),
			'congtrinh_info' => $this->news_info($GLOBALS['cfg']['congtrinh_pageid'])
		);
	}

	private function logo()
	{
		$this->db->select('file, w, h, type');
		$this->db->where('status', 1);
		$this->db->where('cat', 0);
		$query = $this->db->get('interfaces');
		if ($query->num_rows() > 0)
		{
			return $query->row_array();
		}
		else
		{
			return false;
		}
	}

	public function adv_box($target = '', $cat = 3)
	{
		$this->db->select('file, w, h, type, url, images, move_offset, move_effect, speed');
		$this->db->where('status', 1);
		$this->db->where('cat', $cat);
		if ($target) $this->db->where('target', $target);
		$query = $this->db->get('interfaces');
		if ($query->num_rows() > 0)
		{
			return $query->result_array();
		}
		else
		{
			return false;
		}
	}

	public function cat_menu()
	{
		$this->db->query('SET NAMES "LATIN1"');
		$this->db->from('product_categories AS c');
		$this->db->select('c.id, c.parent, c.keyword, c.name_'.$this->lng.' AS name, COUNT(s.parent) AS chirld, c.icon', false);
		$this->db->join('product_categories as s', 'c.id = s.parent', 'left');
		$this->db->order_by('c.sort_order asc');
		$this->db->group_by('id');
		$this->db->where('c.deleted', 0);
		$this->db->where('c.active', 1);
		$query = $this->db->get();
		if ($query->num_rows() > 0)
		{
			return $query->result_array();
		}
		else
		{
			return false;
		}
	}

	private function partners()
	{
		$this->db->query('SET NAMES "LATIN1"');
		$this->db->select('name_'.$this->lng.' AS name, url, icon');
		$this->db->where('active', 1);
		$this->db->where('slider', 1);
		$this->db->where('deleted', 0);
		$this->db->order_by('sort_order desc');
		$query = $this->db->get('trademarks');
		if ($query->num_rows() > 0)
		{
			return $query->result_array();
		}
		else
		{
			return false;
		}
	}

	public function num_item_cart()
	{
		$this->db->where('ip', $this->input->ip_address());
		return $this->db->get('shopping_carts')->num_rows();
	}

	public function list_slider()
	{
		$this->db->query('SET NAMES "LATIN1"');
		$this->db->select('id, keyword, gia2, name_'.$this->lng.' AS name, img, des_'.$this->lng.' AS des, (SELECT keyword FROM product_categories WHERE id = CONCAT("", cat, "")) AS catkey', false);
		$this->db->where('best', 1);
		$this->db->where('active', 1);
		$this->db->where('deleted', 0);
		$query = $this->db->get('products');
		if ($query->num_rows() > 0)
		{
			return $query->result_array();
		}
	}

	public function news_menu($catkey = '', $limit = '', $order_by = '', $type = '')
	{
        $cat = 0;
		if ($catkey)
		{
			$this->db->select('id');
			if (is_numeric($catkey))
			{
				$this->db->where('id', $catkey);
			}
			else
			{
				$this->db->where('keyword', $catkey);
			}
			$query = $this->db->get('article_categories');
			if ($query->num_rows() > 0)
			{
				$cat = $query->row()->id;
			}
		}
		$this->db->query('SET NAMES "LATIN1"');
		$this->db->select('id, keyword, name_'.$this->lng.' AS name, img, des_'.$this->lng.' AS des, cat');
		if ($cat)
		{
			$this->db->where('cat', $cat);
		}
		if ($type)
		{
			$this->db->where($type, 1);
		}
		$this->db->where('deleted', 0);
		$this->db->where('active', 1);
		if ($order_by == '')
		{
			$order_by = 'sort_order';
		}
		$this->db->order_by($order_by.' desc');
		if ($limit)
		{
			$this->db->limit($limit);
		}
		$query = $this->db->get('articles');
		if ($query->num_rows() > 0)
		{
			return $query->result_array();
		}
		else
		{
			return false;
		}
	}

	private function newscat_menu($catkey = '', $limit = '', $order_by = '', $type = '')
	{
		$lng = $GLOBALS['cfg']['lng'];
		if ($catkey)
		{
			$this->db->select('id');
			if (is_numeric($catkey))
			{
				$this->db->where('id', $catkey);
			}
			else
			{
				$this->db->where('keyword', $catkey);
			}
			$this->db->where('deleted', 0);
			$this->db->where('active', 1);
			$query = $this->db->get('article_categories');
			if ($query->num_rows() > 0 && $catkey)
			{
				$cat = $query->row()->id;
			}
		}
		else
		{
			$cat = 0;
		}
		$this->db->query('SET NAMES "LATIN1"');
		$this->db->select('id, keyword, name_'.$this->lng.' AS name');
		$this->db->where('parent', $cat);
		if ($type)
		{
			$this->db->where($type, 1);
		}
		$this->db->where('deleted', 0);
		$this->db->where('active', 1);
		if ($order_by == '')
		{
			$order_by = 'sort_order';
		}
		$this->db->order_by($order_by.' asc');
		if ($limit)
		{
			$this->db->limit($limit);
		}
		$query = $this->db->get('article_categories');
		if ($query->num_rows() > 0)
		{
			return $query->result_array();
		}
		else
		{
			return false;
		}
	}

	public function news_info($id)
	{
		$this->db->query('SET NAMES "LATIN1"');
		$lng = $GLOBALS['cfg']['lng'];
		$this->db->select('id, keyword, name_'.$lng.' AS name, img, des_'.$lng.' AS des, info_'.$lng.' AS info, cat');
		$this->db->where('deleted', 0);
		if (is_numeric($id))
		{
			$this->db->where('id', $id);
		}
		else
		{
			$this->db->where('keyword', $id);
		}
		$query = $this->db->get('articles');
		if ($query->num_rows() > 0)
		{
			return $query->row_array();
		}
		else
		{
			return false;
		}
	}

	public function online_info()
	{
		$data = array();
		$this->db->select('COUNT(id) AS visit');
		$this->db->where('day', date('d'));
		$this->db->where('month', date('m'));
		$this->db->where('year', date('Y'));
		$query = $this->db->get('online');
		$data['visit_today'] = $query->row()->visit;
		$this->db->select('COUNT(id) AS visit');
		$this->db->where('day', date('d', strtotime('-1 days')));
		$this->db->where('month', date('m'));
		$this->db->where('year', date('Y'));
		$query = $this->db->get('online');
		$data['visit_yestorday'] = $query->row()->visit;
		$this->db->select('COUNT(id) AS visit');
		$this->db->where('month', date('m'));
		$this->db->where('year', date('Y'));
		$query = $this->db->get('online');
		$data['this_month'] = $query->row()->visit;
		$this->db->select('COUNT(id) AS visit');
		$query = $this->db->get('online');
		$data['total_visit'] = $query->row()->visit;
		$this->db->select('COUNT(id) AS visit');
		$this->db->where('online_lastactive >=', time() - COOKIE_TIME);
		$this->db->where('day', date('d'));
		$this->db->where('month', date('m'));
		$this->db->where('year', date('Y'));
		$query = $this->db->get('online');
		$data['now_online'] = $query->row()->visit;
		return $data;
	}

	public function region_list()
	{
		$this->db->where('parent', 0);
		$this->db->where('active', 1);
		$this->db->where('deleted', 0);
		$this->db->select('id, name_vn AS name');
		$this->db->order_by('sort_order desc');
		$query = $this->db->get('regions');
		if ($query->num_rows())
		{
			return $query->result_array();
		}
		else
		{
			return false;
		}
	}

	public function profile()
	{
		$this->db->where('email', $GLOBALS['var']['mem_email']);
		return $this->db->get('members')->row_array();
	}

	public function slider_box()
	{
		$this->db->select('file, w, h, type, url, images, move_offset, move_effect, fade_effect, speed');
		$this->db->where('status', 1);
		$this->db->where('cat', 1);
		$query = $this->db->get('interfaces');
		if ($query->num_rows() > 0)
		{
			return $query->result_array();
		}
		else
		{
			return false;
		}
	}

	public function list_tags()
	{
		$this->db->query('SET NAMES "LATIN1"');
		$this->db->select('id, name_'.$this->lng.' AS name, url', false);
		$this->db->where('active', 1);
		$this->db->where('deleted', 0);
		$query = $this->db->get('tags');
		if ($query->num_rows() > 0)
		{
			return $query->result_array();
		}
	}

	public function list_property($property)
	{
		$this->db->query('SET NAMES "LATIN1"');
		$this->db->from('products AS p');
		$this->db->select('p.id, p.size, p.chatlieu, p.dvt, COUNT(p.id) AS product, p1.name_'.$this->lng.' AS name', false);
		$this->db->join('product_properties AS p1', 'p1.id = p.'.$property, 'left');
		$this->db->where('p.active', 1);
		$this->db->where('p.deleted', 0);
		$this->db->group_by('p.'.$property);
		$query = $this->db->get();
		if ($query->num_rows() > 0)
		{
			return $query->result_array();
		}
		else
		{
			return false;
		}
	}

}

/* End of file hiti_common_model.php */
/* Location: ./application/models/hiti_common_model.php */